package com.network.service;

import com.network.dao.UserDao;
import com.network.entity.User;
import com.network.util.CommonUtils;
import com.network.vo.SecurityUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author peng
 */
@Service
public class CustomUserDetailsService implements UserDetailsService
{
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    UserDao userDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {

        logger.info("用户的用户名: {}", username);
        User user=userDao.findFirstByUsername(username);
        SecurityUser securityUser=new SecurityUser();
        BeanUtils.copyProperties(user,securityUser);

        if (CommonUtils.isNull(securityUser))
        {
            throw  new UsernameNotFoundException("用户名不存在");
        }

        return securityUser;
    }
}
